package com.ruoyi.business.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.business.payment.service.IThirdPaymentService;
import com.ruoyi.business.service.*;
import com.ruoyi.business.utils.JwtUtil;
import com.ruoyi.business.vo.OpenApiReqVo;
import com.ruoyi.business.vo.OpenApiTokenVo;
import com.ruoyi.business.vo.TRegisterXXQYVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.base.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 对外接口Controller
 *
 * @author sgchen
 * @date 2024-01-24
 */
@Api(tags = "对外接口Controller")
@RestController
@RequestMapping("/openapi/registerInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OpenApiController extends BaseController {

    private final ITRegisterInfoService tRegisterInfoService;

    private final IThirdPaymentService thirdPaymentService;

    /**
     * 获取token
     * @param apiReqVo
     * @return
     */
    @ApiOperation("获取token")
    @PostMapping("getToken")
    public AjaxResult getToken(@RequestBody @Validated OpenApiReqVo apiReqVo) {
        // 先写死学习强医的appid，xxqy
        if(!StringUtils.equals(apiReqVo.getAppid(),"xxqy")){
          return AjaxResult.error("appId不存在");
        }
        String token = JwtUtil.generateToken(apiReqVo.getAppid());
        return AjaxResult.success("操作成功",token);
    }


    /**
     * 学习强医数据同步
     * @param tokenVo
     * @return
     */
    @ApiOperation("学习强医数据同步")
    @PostMapping("getXXQYRegisterList")
    public TableDataInfo<TRegisterXXQYVo> getXXQYRegisterList(@RequestBody @Validated OpenApiTokenVo tokenVo) {
        boolean validateToken = JwtUtil.validateToken(tokenVo.getToken());
        if(!validateToken){
           throw new BaseException("token不存在或者失效");
        }
        return tRegisterInfoService.getXXQYRegisterList(tokenVo);
    }
    /**
     * 学习强医数据同步,已结业数据
     * @param tokenVo
     * @return
     */
    @ApiOperation("学习强医数据同步,已结业数据")
    @PostMapping("getXXQYGradRegisterList")
    public TableDataInfo<TRegisterXXQYVo> getXXQYGradRegisterList(@RequestBody @Validated OpenApiTokenVo tokenVo) {
        boolean validateToken = JwtUtil.validateToken(tokenVo.getToken());
        if(!validateToken){
            throw new BaseException("token不存在或者失效");
        }
        return tRegisterInfoService.getXxqyJyRegisterList(tokenVo);
    }

    @ApiOperation("支付完成回调接口")
    @PostMapping("paymentCallback")
    public JSONObject paymentCallback() {
        return thirdPaymentService.paymentCallback();
    }

    @ApiOperation("退款完成回调接口")
    @PostMapping("refundCallback")
    public JSONObject refundCallback() {
        return thirdPaymentService.refundCallback();
    }

}
